#!/usr/bin/ruby

require 'bio'

require 'soap/wsdlDriver'
wsdl = "http://soap.genome.jp/KEGG.wsdl"

serv = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver

serv.generate_explicit_type = true

#links = serv.get_linkdb_between_databases("eco", "pathway", 1, 100)
#links = serv.get_linkdb_by_entry("md:M00001", "orthology", 1, 1000)


query = ARGV[0]

mod = ARGV[1]

# object for KO query
ko = serv.bget("orthology:#{query}")

# object for Module query
entry = serv.bget("md:#{mod}")

korth = Bio::KEGG::ORTHOLOGY.new(ko).dblinks_as_hash

name = Bio::KEGG::ORTHOLOGY.new(ko).entry_id

keys = korth.keys
values = korth.values

for key in 0..keys.length
  if keys[key] == "COG"
    print name, "\t", values[key], "\n"
  end  
end

print "END of test...\n\n"

#puts entry 

orthologs = Bio::KEGG::MODULE.new(entry).orthologs_as_array

orthologs.each do |orth|
  o = serv.bget("orthology:#{orth}")
  n = Bio::KEGG::ORTHOLOGY.new(o).entry_id
  i = Bio::KEGG::ORTHOLOGY.new(o).dblinks_as_hash
  keys = i.keys
  values = i.values
  for key in 0..keys.length
    if keys[key] == "COG"
      print n, "\t", values[key], "\n"
    end
  end
end
